﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace MemoryAid
{
    public partial class Form1 : Form
    {
        const string FILENAME = "MemoryAid.txt";
        const string DATASPLIT = "\n#################\n\n";
        const string LISTSPLIT = "\n#######\n";
        const string WORDSPLIT = "\n###\n";
        struct MemoryList
        {
            public String key;
            public String comment;
        }
        public Form1()
        {
            DialogResult dr = MessageBox.Show("建立新课程？", "MemoryAid", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            InitializeComponent();
            try
            {
                String fContent = File.ReadAllText(fileName);
                String[] fList = fContent.Split(new String[]{DATASPLIT},StringSplitOptions.None);
                total = fList.Length;
                for (int i = 0; i < total; i++)
                {
                    List<MemoryList> tMList = new List<MemoryList>();
                    String[] mList = fList[i].Split(new String[] {LISTSPLIT}, StringSplitOptions.RemoveEmptyEntries);
                    for (int j = 0; j < mList.Length; j++)
                    {
                        MemoryList ml = new MemoryList();
                        String[] tml = mList[j].Split(new String[] {WORDSPLIT}, StringSplitOptions.None);
                        ml.key = "";
                        ml.comment = "";
                        if (tml.Length >= 2)
                            ml.comment = tml[1];
                        if (tml.Length >= 1)
                            ml.key = tml[0];
                        if(ml.key != "")
                            tMList.Add(ml);
                    }
                    contentList.Add(tMList);
                }
            }
            catch
            {
                total = 0;
            }
            if (dr == DialogResult.OK || total == 0)
            {
                List<MemoryList> tMList = new List<MemoryList>();
                contentList.Add(tMList);
                total++;
                saveToFile();
            }
            if (total == 1)
                offset = 0;
            else
                offset = 1;
            current = total - 1;
            review = current - ((1 << offset) - 1);
            curpage = 0;
            revpage = 0;
            restoreCurrent();
            restoreReview();
            textBox6.Select();
        }
        string fileName = FILENAME;
        List<List<MemoryList>> contentList = new List<List<MemoryList>>();
        int current,review,total,offset,curpage,revpage;
        void saveToFile()
        {
            int trytime = -1;
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < contentList.Count; i++)
            {
                for (int j = 0; j < contentList[i].Count; j++)
                {
                    sb.Append(contentList[i][j].key);
                    sb.Append(WORDSPLIT);
                    sb.Append(contentList[i][j].comment);
                    if (j != contentList[i].Count - 1)
                        sb.Append(LISTSPLIT);
                }
                if (i != contentList.Count - 1)
                    sb.Append(DATASPLIT);
            }
        sfTable:
            try
            {
                File.WriteAllText(fileName, sb.ToString());
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(fileName + "创建不了！");
                trytime++;
                fileName = FILENAME + trytime;
                if (trytime < 5)
                    goto sfTable;
            }
        }
        public void restoreCurrent()
        {
            label2.Text = total.ToString();
            textBox1.Text = current.ToString();
            label10.Text = contentList[current].Count.ToString();
            textBox3.Text = curpage.ToString();
            if (curpage < contentList[current].Count)
            {
                textBox6.Text = contentList[current][curpage].key;
                //textBox7.Text = contentList[current][curpage].comment;
                textBox7.Text = "";
            }
            else
            {
                textBox6.Text = "";
                textBox7.Text = "";
            }
        }

        public void restoreReview()
        {
            textBox5.Text = offset.ToString();
            textBox2.Text = review.ToString();
            label12.Text = contentList[review].Count.ToString();
            textBox4.Text = revpage.ToString();
            if (revpage < contentList[review].Count)
            {
                textBox8.Text = contentList[review][revpage].key;
                //textBox9.Text = contentList[review][revpage].comment;
                textBox9.Text = "";
            }
            else
            {
                textBox8.Text = "";
                textBox9.Text = "";
            }
        }

        private void key_Press(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != 8)
                e.Handled = true;
        }
        void subCurrent()
        {
            current--;
            if (current < 0)
                current = total - 1;
        }
        private void button13_Click(object sender, EventArgs e)
        {
            subCurrent();
            curpage = 0;
            restoreCurrent();
        }
        void addCurrent()
        {
            current++;
            if (current >= total)
                current = 0;
        }
        private void button11_Click(object sender, EventArgs e)
        {
            addCurrent();
            curpage = 0;
            restoreCurrent();
        }
        void subCurpage()
        {
            curpage--;
            if (curpage < 0)
            {
                if (contentList[current].Count > 0)
                    curpage = contentList[current].Count - 1;
                else
                    curpage = 0;
            }
        }
        private void button8_Click(object sender, EventArgs e)
        {
            subCurpage();
            restoreCurrent();
        }
        void addCurpage()
        {
            curpage++;
            if (curpage >= contentList[current].Count)
                curpage = 0;
        }
        private void button7_Click(object sender, EventArgs e)
        {
            addCurpage();
            restoreCurrent();
        }

        private void button21_Click(object sender, EventArgs e)
        {
            restoreCurrent();
        }

        private void button23_Click(object sender, EventArgs e)
        {
            int num;
            try
            {
                num = int.Parse(textBox1.Text);
                if (num < 0 || num >= total)
                    num = current;
            }
            catch (System.Exception ex)
            {
                num = current;
            }
            current = num;

            try
            {
                num = int.Parse(textBox3.Text);
                if (num < 0 || num >= contentList[current].Count)
                    num = curpage;
            }
            catch (System.Exception ex)
            {
                num = curpage;
            }
            curpage = num;
            restoreCurrent();
        }

        private void button24_Click(object sender, EventArgs e)
        {
            textBox6.Text = "";
            textBox7.Text = "";
        }

        private void button16_Click(object sender, EventArgs e)
        {
            textBox7.Text = contentList[current][curpage].comment;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            textBox7.Text = "";
        }

        private void button17_Click(object sender, EventArgs e)
        {
            
            if (curpage < contentList[current].Count)
            {
                DialogResult dr = MessageBox.Show("确定要删除？", "MemoryAid", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (dr == DialogResult.OK)
                {
                    contentList[current].RemoveAt(curpage);
                    subCurpage();
                    restoreCurrent();
                    saveToFile();
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (textBox6.Text != "")
            {
                MemoryList ml = new MemoryList();
                ml.key = textBox6.Text;
                ml.comment = textBox7.Text;
                contentList[current].Add(ml);
                restoreCurrent();
                textBox6.Text = "";
                saveToFile();
                textBox6.Select();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox6.Text != "")
            {
                DialogResult dr = MessageBox.Show("确定要修改？", "MemoryAid", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (dr == DialogResult.OK)
                {
                    MemoryList ml = new MemoryList();
                    ml.key = textBox6.Text;
                    ml.comment = textBox7.Text;
                    List<MemoryList> mList = contentList[current];
                    mList[curpage] = ml;
                    contentList[current] = mList;
                    saveToFile();
                }
            }
        }
        void subReview()
        {
            review--;
            if (review < 0)
                review = total - 1;
        }
        private void button14_Click(object sender, EventArgs e)
        {
            subReview();
            revpage = 0;
            restoreReview();
        }
        void addReview()
        {
            review++;
            if (review >= total)
                review = 0;
        }
        private void button12_Click(object sender, EventArgs e)
        {
            addReview();
            revpage = 0;
            restoreReview();
        }
        void subRevpage()
        {
            revpage--;
            if (revpage < 0)
            {
                if (contentList[review].Count > 0)
                    revpage = contentList[review].Count - 1;
                else
                    revpage = 0;
            }
        }
        private void button10_Click(object sender, EventArgs e)
        {
            subRevpage();
            restoreReview();
        }
        void addRevpage()
        {
            revpage++;
            if (revpage >= contentList[review].Count)
                revpage = 0;
        }
        private void button9_Click(object sender, EventArgs e)
        {
            addRevpage();
            restoreReview();
        }

        private void button26_Click(object sender, EventArgs e)
        {
            restoreReview();
        }

        private void button27_Click(object sender, EventArgs e)
        {
            int num;
            try
            {
                num = int.Parse(textBox2.Text);
                if (num < 0 || num >= total)
                    num = review;
            }
            catch (System.Exception ex)
            {
                num = review;
            }
            review = num;

            try
            {
                num = int.Parse(textBox4.Text);
                if (num < 0 || num >= contentList[review].Count)
                    num = revpage;
            }
            catch (System.Exception ex)
            {
                num = revpage;
            }
            revpage = num;
            try
            {
                num = int.Parse(textBox5.Text);
                if (num > 30 || num < 0 || ((1<<num) - 1) > current)
                    num = offset;
            }
            catch (System.Exception ex)
            {
                num = offset;
            }
            if (num != offset)
            {
                offset = num;
                review = current - (1 << offset) + 1;
                revpage = 0;
            }
            restoreReview();
        }

        private void button25_Click(object sender, EventArgs e)
        {
            textBox8.Text = "";
            textBox9.Text = "";
        }

        private void button15_Click(object sender, EventArgs e)
        {
            textBox9.Text = contentList[review][revpage].comment;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            textBox9.Text = "";
        }

        private void button18_Click(object sender, EventArgs e)
        {
            if (revpage < contentList[review].Count)
            {
                DialogResult dr = MessageBox.Show("确定要删除？", "MemoryAid", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (dr == DialogResult.OK)
                {
                    contentList[review].RemoveAt(revpage);
                    subRevpage();
                    restoreReview();
                    saveToFile();
                }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (textBox8.Text != "")
            {
                DialogResult dr = MessageBox.Show("确定要添加？", "MemoryAid", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (dr == DialogResult.OK)
                {
                    MemoryList ml = new MemoryList();
                    ml.key = textBox8.Text;
                    ml.comment = textBox9.Text;
                    contentList[review].Add(ml);
                    restoreReview();
                    saveToFile();
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox8.Text != "")
            {
                DialogResult dr = MessageBox.Show("确定要修改？", "MemoryAid", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (dr == DialogResult.OK)
                {
                    MemoryList ml = new MemoryList();
                    ml.key = textBox8.Text;
                    ml.comment = textBox9.Text;
                    List<MemoryList> mList = contentList[review];
                    mList[revpage] = ml;
                    contentList[review] = mList;
                    saveToFile();
                }
            }
        }
        void subOffset()
        {
            offset--;
            if (offset < 0)
            {
                int i;
                for (i = 30; i >= 0; i--)
                {
                    if ((1 << i) - 1 <= current)
                        break;
                }
                offset = i;
            }
        }
        private void button20_Click(object sender, EventArgs e)
        {
            if (revpage > 0)
            {
                revpage--;
                restoreReview();
            }
            else
            {
                subOffset();
                review = current - (1 << offset) + 1;
                if (contentList[review].Count > 0)
                    revpage = contentList[review].Count - 1;
                else
                    revpage = 0;
                restoreReview();
            }
        }
        void addOffset()
        {
            offset++;
            if ((1 << offset) - 1 > current)
                offset = 0;
        }
        private void button19_Click(object sender, EventArgs e)
        {
            if (revpage < contentList[review].Count - 1)
            {
                revpage++;
                restoreReview();
            }
            else
            {
                addOffset();
                review = current - (1 << offset) + 1;
                revpage = 0;
                restoreReview();
            }
        }

        private void button22_Click(object sender, EventArgs e)
        {
            if (offset > 0 && revpage < contentList[review].Count)
            {
                MemoryList ml = new MemoryList();
                ml.key = contentList[review][revpage].key;
                ml.comment = contentList[review][revpage].comment;
                contentList[current].Add(ml);
                contentList[review].RemoveAt(revpage);
                if (revpage >= contentList[review].Count)
                {
                    if (contentList[review].Count > 0)
                        revpage = contentList[review].Count - 1;
                    else
                        revpage = 0;
                }
                restoreReview();
                saveToFile();
            }
        }

        private void button28_Click(object sender, EventArgs e)
        {
            List<MemoryList> tMList = new List<MemoryList>();
            contentList.Add(tMList);
            total++;
            if (total == 1)
                offset = 0;
            else
                offset = 1;
            current = total - 1;
            review = current - ((1 << offset) - 1);
            curpage = 0;
            revpage = 0;
            restoreCurrent();
            restoreReview();
            saveToFile();
            textBox6.Select();
        }
    }
}
